$(document).ready(function(){
	var type=$('#type').val();
	var scheduId=$('#scheduId').val();
	//初始化form表单
	initForm(type,scheduId);
	
	//事件绑定
	eventBind(type,scheduId);
});

//初始化form表单
function initForm(type,scheduId){
	$('#scheduForm').validation();
	//初始化日历控件
//	initDatePickers();
	
	if(type=='add'){
		var date = addDate.split(' ')[0];
		var hour = addDate.split(' ')[1].split(':')[0];
		var minute = addDate.split(' ')[1].split(':')[1];
		//初始化排班科室下拉框
		initScheduDeptSel(null,null);
		//初始化开始结束小时下拉框
		initHourSel(hour,null);
		//初始化开始结束分钟下拉框
		initMinitueSel(minute,null);
		//设置默认排班日期
		$("input[name='schedu_date']").val(date);
		$("#start_hour").multipleSelect("disable");
		$("#start_minitue").multipleSelect("disable");
	}else{
		$('#delete').show();
		$.getApiClient('/scheduling/{scheduId}').setQuery({scheduId: scheduId}).get().done(function(data){
			if(data.code=='0'){
				var payload=data.payload;
				$('#scheduForm').bindData(payload);
				$("input[name='schedu_date']").val($.dateFormat(new Date(payload.schedu_date), "yyyy-MM-dd"));
				//初始化排班科室下拉框
				initScheduDeptSel(payload.schedu_did,payload.device_id);
				
				var startArray=payload.schedu_start.split(":");
				var endArray=payload.schedu_end.split(":");
				//初始化开始结束小时下拉框
				initHourSel(startArray[0],endArray[0]);
				//初始化开始结束分钟下拉框
				initMinitueSel(startArray[1],endArray[1]);
			}
		})
	}
	
	//初始化排班科室下拉框
	function initScheduDeptSel(defaultDeptVal,defaultDeviceVal){
		$.getApiClient('/scheduling/depts').get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("form_schedu_did",[{"id": "", "department_name": "请选择"}].concat(data.payload), defaultDeptVal, {valueAttr:'id', displayAttr:'department_name'},"请选择");
			$('#form_schedu_did').multipleSelect({width:'100%', single: true});
			
			$('#form_schedu_did').change(function(){
				var seldeptId=$(this).children('option:selected').val();
				initScheduDeviceSel(seldeptId,defaultDeviceVal);
			}).change();
		});
	}
	
	//初始化排班设备下拉框
	function initScheduDeviceSel(seldeptId,defaultVal){
		if(seldeptId){
			$.getApiClient('/scheduling/devices').setQuery({department_id:seldeptId}).get().done(function(data){
				if(!data.payload){
					data.payload=[];
				}
			    multipleSelectData("form_device_id",[{"id": "", "device_name": "请选择"}].concat(data.payload), defaultVal, {valueAttr:'id', displayAttr:'device_name'},"请选择");
				$('#form_device_id').multipleSelect({width:'100%', single: true});
				
				if(type=='edit'){
					$("#form_schedu_did").multipleSelect("disable");
					$("#form_device_id").multipleSelect("disable");
				}
			});
		}else{
			multipleSelectData("form_device_id",[{"id": "", "device_name": "请选择"}], defaultVal, {valueAttr:'id', displayAttr:'device_name'},"请选择");
			$('#form_device_id').multipleSelect({width:'100%', single: true});
		}
	}
	
	//初始化开始结束小时下拉框
	function initHourSel(defaultStartHour,defaultEndHour){
		var conHourArry=[];
		for(var i=8;i<=20;i++){
			var hourItem={};
			if(i<10){
				hourItem.code="0"+i;
				hourItem.value="0"+i;
			}else{
				hourItem.code=i;
				hourItem.value=i;
			}
			conHourArry.push(hourItem);
		}
		
		if(defaultStartHour){
			multipleSelectData("start_hour",conHourArry,defaultStartHour);
			$('#start_hour').multipleSelect({width:'20%', single: true});
		}else{
			multipleSelectData("start_hour",conHourArry,"08");
			$('#start_hour').multipleSelect({width:'20%', single: true});
		}

		if(defaultEndHour){
			multipleSelectData("end_hour",conHourArry,defaultEndHour);
			$('#end_hour').multipleSelect({width:'20%', single: true});
		}else{
			multipleSelectData("end_hour",conHourArry,"08");
			$('#end_hour').multipleSelect({width:'20%', single: true});
		}
	}
	
	//初始化开始结束分钟下拉框
	function initMinitueSel(defaultStartMin,defaultEndMin){
		var conMinitueArry=[{code:'00',value:'00'}];
		for(var i=30;i<60;i=i+30){
			conMinitueArry.push({code:i+'',value:i+''});
		}
		
		if(defaultStartMin){
			multipleSelectData("start_minitue",conMinitueArry,defaultStartMin);
			$('#start_minitue').multipleSelect({width:'22%', single: true});
		}else{
			multipleSelectData("start_minitue",conMinitueArry,"00");
			$('#start_minitue').multipleSelect({width:'22%', single: true});
		}
		
		if(defaultEndMin){
			multipleSelectData("end_minitue",conMinitueArry,defaultEndMin);
			$('#end_minitue').multipleSelect({width:'22%', single: true});
		}else{
			multipleSelectData("end_minitue",conMinitueArry,"00");
			$('#end_minitue').multipleSelect({width:'22%', single: true});
		}
	}
}

//事件绑定
function eventBind(type,scheduId){
	$('#cancel').click(function(){
		closeAllDialog();
	})
	
	if(type=='add'){
		$('#save').on('click',saveSchedu);
	}else{
		$('#save').on('click',function(){
			updateSchedu(scheduId);
		});
		
		$('#delete').on('click',function(){
			showDelFormDialog(scheduId);
		});
	}
}

//新增排班操作
function saveSchedu(){
	if(!$("#scheduForm").validationForm()){
		return;
	}
	
	if(!$('#form_schedu_did').multipleSelect('getSelects')[0]){
		customAlertDialog("排班科室需填写", '错误提示', null, null);
		return;
	}
	
	if(!$('#form_device_id').multipleSelect('getSelects')[0]){
		customAlertDialog("排班设备需填写", '错误提示', null, null);
		return;
	}
	
	if(!checkTimeRange()){
		customAlertDialog("排班区间起始时间不能晚于结束时间", '错误提示', null, null);
		return;
	}
	
	$.getApiClient('/scheduling').post('#scheduForm', function(params){
		return $.extend(params, {
			schedu_start:params.start_hour+':'+params.start_minitue,
			schedu_end:params.end_hour+':'+params.end_minitue,
			schedu_type:schedu_type
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','保存成功',null,2000);
			//触发查询
			$("#queryBtn").click();
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}

function checkTimeRange(){
	var checkResult=false;
	var startHour = $('#start_hour').children('option:selected').val();
	var startMin = $('#start_minitue').children('option:selected').val();
	var endHour = $('#end_hour').children('option:selected').val();
	var endMin = $('#end_minitue').children('option:selected').val();
	
	if(Number(startHour)< Number(endHour)){
		checkResult=true;
	}else if(Number(startHour)> Number(endHour)){
		checkResult=false;
	}else if(Number(startHour)== Number(endHour)){
		if(Number(startMin)>= Number(endMin)){
			checkResult=false;
		}else{
			checkResult=true;
		}
	}
	
	return checkResult;
}

//修改排班操作
function updateSchedu(scheduId){
	if(!$("#scheduForm").validationForm()){
		return;
	}
	
	if(!$('#form_schedu_did').multipleSelect('getSelects')[0]){
		customAlertDialog("排班科室需填写", '错误提示', null, null);
		return;
	}
	
	if(!$('#form_device_id').multipleSelect('getSelects')[0]){
		customAlertDialog("排班设备需填写", '错误提示', null, null);
		return;
	}
	
	if(!checkTimeRange()){
		customAlertDialog("排班区间起始时间不能晚于结束时间", '错误提示', null, null);
		return;
	}
	
	$.getApiClient('/scheduling/{scheduId}').setQuery({
		scheduId:scheduId
	}).put('#scheduForm', function(params){
		return $.extend(params, {
			schedu_start:params.start_hour+':'+params.start_minitue,
			schedu_end:params.end_hour+':'+params.end_minitue,
			schedu_type:schedu_type
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','修改成功',null,2000);
			//触发查询
			$("#queryBtn").click();
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}

//删除排班弹出
function showDelFormDialog(scheduId){
	customConfirmDialog('是否确认删除该排班？','删除排班',null,function(dialog){
		//执行删除
		closeAllDialog();
		doDeleteSchedu(scheduId,function(data){
			if(data.code == "0"){
				customTimeOutDialog('信息提示','删除排班成功',null,2000);
				//触发查询
				$("#queryBtn").click();
			}else{
				customAlertDialog(data.message, '错误提示', null, null);
			}
		})
	});
	
	function doDeleteSchedu(scheduId,callBack){
		$.getApiClient('/scheduling/{scheduId}').setQuery({
			scheduId : scheduId
		}).del().done(function(data){
			callBack(data)
		});
	}
}